package controller;

        import javafx.event.ActionEvent;
        import javafx.fxml.FXML;
        import javafx.scene.control.*;
        import javafx.scene.layout.BorderPane;
        import javafx.scene.layout.GridPane;
        import javafx.scene.layout.HBox;
        import javafx.scene.layout.VBox;
        import service.*;
        import service.impl.*;
        import utill.*;
        import utill.component.promptBox;

        import java.io.IOException;

public class loginController {
    CustomerService  customerService =new CustomerServiceImpl();
    FlowerStoreService flowerStoreService=new FlowerStoreServiceImpl();

    @FXML
    private PasswordField password;

    @FXML
    private Button btncommit;

    @FXML
    private Label Textpassword;

    @FXML
    private HBox loginButtonVbox;

    @FXML
    private BorderPane loginBorderPane;

    @FXML
    private VBox loginLabeVbox;

    @FXML
    private VBox loginFiledVbox;

    @FXML
    private ToggleButton btnChange;

    @FXML
    private Label loginTitle;

    @FXML
    private TextField userName;

    @FXML
    private Label TextuserName;

    @FXML  GridPane loginGridPane;

    boolean sign=false;
    String  customerUri="/controller/customerSelect.fxml";
    String  storeUri="/controller/storeManage.fxml";
    int     count=1;

    @FXML
    void ObjectChange(ActionEvent event) throws IOException {
        if(count%2==1){
            loginTitle.setText("商家登录");
            this.sign=true;
        }else{
            loginTitle.setText("用户登录");
            this.sign=false;
        }
        count++;
    }

    @FXML
    void loginCommit(ActionEvent event) throws IOException {
           if(this.sign)
               login(event,storeUri,this.sign);
           else
               login(event,customerUri,this.sign);
        Thread t =Thread.currentThread();
        String name =t.getName();
        System.out.println(ParamUtil.userId.get(name+"userId"));

    }

    public void login(ActionEvent event ,String uri ,boolean flag) throws IOException {
        boolean identification=false;
        switchpages switchpages =new switchpages();
        if(flag==false){
            identification=customerService.customerLogin(userName.getText() ,password.getText());
        }else{
            identification=flowerStoreService.flowerStoreLogin(userName.getText(),password.getText());
        }

        if(identification){
            switchpages.switch_view(event,uri);
        }else{
            promptBox.tipsMessage("用户名或密码错误");
            userName.setText("");
            password.setText("");
        }
    }
}

